#include "esp8266.h"
#include "iosensor.h"
#include "driver_tool.h"
#include "adc.h"
#include "gpio.h"
#include "main.h"
#include "driver_adc.h"
#include "driver_delay.h"
#include "driver_tool.h"
#include "driver_adc.h"
#include "driver_usart.h"
#include "driver_delay.h"
#include "driver_task.h"
#include "driver_key.h"
#include <stdio.h>
#include <string.h>

#include "main.h"
#include "cmsis_os.h"
#include "adc.h"
#include "dma.h"
#include "i2c.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

#include "driver_usart.h"


#define ESP_AT_TEST "AT\r\n" // 测试AT启动
#define ESP_SET_STA "AT+CWMODE=1\r\n" // 设置Wi-Fi模式为 STA模式
#define ESP_CLOSE_AP "AT+CWAUTOCONN=0\r\n" // 开机后自动连接AP关闭，取消自动连接
#define ESP_CONNECT_AP "AT+CWJAP=\"xiaogua\",\"88888888\"\r\n" // 连接到AP，"热点名称","密码"
#define ESP_SET_SERIAL_NET "AT+CIPMODE=1\r\n" // 配置传输模式为 透传模式
#define ESP_CLOSE_MULTI "AT+CIPMUX=0\r\n" // 配置多重连接模式关闭，关闭多路连接

/* 与TCP / IP相关的AT命令列表，连接服务器指令 */

#define ESP_CONNECT_SOCKET "AT+CIPSTART=\"TCP\",\"124.220.188.130\",8080\r\n"  // 建立TCP连接
#define ESP_SET_TRANS "AT+CIPSEND\r\n"  // 发送数据，进入透传模式
#define ESP_CLOSE_TRANS "+++"  // 退出透传，不要有发送新行

#define JSON "{\"user\":\"bowuguan\",\"hum\":12,\"temp\":12,\"peo\":0,\"sta\":1}"

int32_t wait_data(uint32_t timeout)
{
	uint32_t temp = 0;

    while (1) {
        if(Rx2_Flag){  // Receive flag
			Rx2_Flag=0;	// clean flag
            HAL_UART_Transmit(&huart1, Rx2_Buf, Rx2_Len, 0xFFFF);
            if(strstr((char *)Rx2_Buf,"OK")!=NULL) {
                GUA_LOGI("found!\n");
            } else {
                GUA_LOGE("not found!\n");
            }
            delay_ms(800);
            return 0;
		}
        temp++;
        delay_ms(1);
        if (temp > timeout) {
            temp = 0;
            GUA_LOGE("ESP8266 ERROR");
            return -1;
        }
    }
}
int32_t inti_esp8266(void)
{
    uint32_t temp = 0;

    GUA_LOGI("rest esp");
    HAL_GPIO_WritePin(ESP_RST_GPIO_Port, ESP_RST_Pin, GPIO_PIN_RESET);
    delay_ms(200);
    HAL_GPIO_WritePin(ESP_RST_GPIO_Port, ESP_RST_Pin, GPIO_PIN_SET);
    delay_ms(1000);
    GUA_LOGI("connect esp...");
    while (1) {
        if(Rx2_Flag){  // Receive flag
			Rx2_Flag=0;	// clean flag
            HAL_UART_Transmit(&huart1, Rx2_Buf, Rx2_Len, 0xFFFF);
        }
        delay_ms(1);
        temp++;
        if (temp >2000) {
            GUA_LOGI("connecting...");
            break;;
        }
    }


    HAL_UART_Transmit(&huart2, (const uint8_t *)ESP_AT_TEST, strlen(ESP_AT_TEST), 0xFFFF);
    HAL_UART_Transmit(&huart1, (const uint8_t *)ESP_AT_TEST, strlen(ESP_AT_TEST), 0xFFFF);
    wait_data(2000);

    HAL_UART_Transmit(&huart2, (const uint8_t *)ESP_SET_STA, strlen(ESP_SET_STA), 0xFFFF);
    HAL_UART_Transmit(&huart1, (const uint8_t *)ESP_SET_STA, strlen(ESP_SET_STA), 0xFFFF);
    wait_data(2000);

    HAL_UART_Transmit(&huart2, (const uint8_t *)ESP_CLOSE_AP, strlen(ESP_CLOSE_AP), 0xFFFF);
    HAL_UART_Transmit(&huart1, (const uint8_t *)ESP_CLOSE_AP, strlen(ESP_CLOSE_AP), 0xFFFF);
    wait_data(2000);

    HAL_UART_Transmit(&huart2, (const uint8_t *)ESP_CONNECT_AP, strlen(ESP_CONNECT_AP), 0xFFFF);
    HAL_UART_Transmit(&huart1, (const uint8_t *)ESP_CONNECT_AP, strlen(ESP_CONNECT_AP), 0xFFFF);
    temp = 0;
    while (1) {
        if(Rx2_Flag){  // Receive flag
			Rx2_Flag=0;	// clean flag
            HAL_UART_Transmit(&huart1, Rx2_Buf, Rx2_Len, 0xFFFF);
            if(strstr((char *)Rx2_Buf,"OK")!=NULL) {
                GUA_LOGI("found!\n");
                break;
            } else {
                GUA_LOGE("not found!\n");
            }
        }
        delay_ms(1);
        temp++;
        if (temp >15000) {
            GUA_LOGE("connect to esp8266 timeout");
            return -1;
        }
    }



    HAL_UART_Transmit(&huart2, (const uint8_t *)ESP_SET_SERIAL_NET, strlen(ESP_SET_SERIAL_NET), 0xFFFF);
    HAL_UART_Transmit(&huart1, (const uint8_t *)ESP_SET_SERIAL_NET, strlen(ESP_SET_SERIAL_NET), 0xFFFF);
    temp = 0;
    while (1) {
        if(Rx2_Flag){  // Receive flag
			Rx2_Flag=0;	// clean flag
            HAL_UART_Transmit(&huart1, Rx2_Buf, Rx2_Len, 0xFFFF);
            if(strstr((char *)Rx2_Buf,"OK")!=NULL) {
                GUA_LOGI("found!\n");
                break;
            } else {
                GUA_LOGE("not found!\n");
            }
        }
        delay_ms(1);
        temp++;
        if (temp >10000) {
            GUA_LOGE("connect to esp8266 timeout");
            return -1;
        }
    }



    HAL_UART_Transmit(&huart2, (const uint8_t *)ESP_CLOSE_MULTI, strlen(ESP_CLOSE_MULTI), 0xFFFF);
    HAL_UART_Transmit(&huart1, (const uint8_t *)ESP_CLOSE_MULTI, strlen(ESP_CLOSE_MULTI), 0xFFFF);
    temp = 0;
    while (1) {
        if(Rx2_Flag){  // Receive flag
			Rx2_Flag=0;	// clean flag
            HAL_UART_Transmit(&huart1, Rx2_Buf, Rx2_Len, 0xFFFF);
            if(strstr((char *)Rx2_Buf,"OK")!=NULL) {
                GUA_LOGI("found!\n");
                break;
            } else {
                GUA_LOGE("not found!\n");
            }
        }
        delay_ms(1);
        temp++;
        if (temp >15000) {
            GUA_LOGE("connect to esp8266 timeout");
            return -1;
        }
    }

    // 连接服务器
    HAL_UART_Transmit(&huart2, (const uint8_t *)ESP_CONNECT_SOCKET, strlen(ESP_CONNECT_SOCKET), 0xFFFF);
    HAL_UART_Transmit(&huart1, (const uint8_t *)ESP_CONNECT_SOCKET, strlen(ESP_CONNECT_SOCKET), 0xFFFF);
    temp = 0;
    while (1) {
        if(Rx2_Flag){  // Receive flag
			Rx2_Flag=0;	// clean flag
            HAL_UART_Transmit(&huart1, Rx2_Buf, Rx2_Len, 0xFFFF);
            if(strstr((char *)Rx2_Buf,"OK")!=NULL) {
                GUA_LOGI("found!\n");
                break;
            } else {
                GUA_LOGE("not found!\n");
            }
        }
        delay_ms(1);
        temp++;
        if (temp >25000) {
            GUA_LOGE("connect to esp8266 timeout");
            return -1;
        }
    }

    // 进入透传模式
    HAL_UART_Transmit(&huart2, (const uint8_t *)ESP_SET_TRANS, strlen(ESP_SET_TRANS), 0xFFFF);
    HAL_UART_Transmit(&huart1, (const uint8_t *)ESP_SET_TRANS, strlen(ESP_SET_TRANS), 0xFFFF);

		return 0;
}

int32_t esp_send_data(char *jsondata)
{
    // 发送数据
    HAL_UART_Transmit(&huart2, (const uint8_t *)jsondata, strlen(jsondata), 0xFFFF);
    // HAL_UART_Transmit(&huart1, jsondata, strlen(jsondata), 0xFFFF);
    // printf("\r\n");

    return 0;
}


